PlayerData = {};
PeerData = {};//Index with player id
function PlayerDataSync( handler, id, encoded, decoded )

	PlayerData = decoded;
	InventoryRefresh();
	
end
datastream.Hook( "PlayerDataSync", PlayerDataSync );

function InventoryOpenOverview()

	InventoryFrame = vgui.Create("DFrame");
	InventoryFrame:SetSize(600,400);
	InventoryFrame:Center();
	InventoryFrame:SetTitle("Inventory");
	InventoryFrame:SetSizable(false);
	InventoryFrame:SetDeleteOnClose(true);
	InventoryFrame:MakePopup();

	InventoryRefresh();
	
end
concommand.Add("InventoryOpen", InventoryOpenOverview);
usermessage.Hook("InventoryOpen", InventoryOpenOverview);

function InventoryRefresh()

	if (!InventoryFrame) then
		return;
	end

	if (InventoryGrid) then
		InventoryGrid:Remove();
	end

	InventoryGrid = vgui.Create("DGrid", InventoryFrame);
	InventoryGrid:SetPos(5,27);
	InventoryGrid:SetCols(5);
	InventoryGrid:SetColWide(64);
	InventoryGrid:SetRowHeight(64);
	
	for key, tab in pairs( PlayerData["Characters"][PlayerData["SelectedCharacter"]]["Inventory"] ) do
	
		local Item = vgui.Create("SpawnIcon", InventoryFrame);
		
			Item:SetSize(64,64);
			Item:SetModel(tab.Model);
			Item:SetToolTip(tab.ShortInfo);
			Item.OnMousePressed = function() 
			
				local Options = DermaMenu();
				
				if tab.Wearable then	
				
					Options:AddOption("Wear", function() RunConsoleCommand("ItemHandleUsage", "Wear".." "..key);InventoryRefresh(); end );
					
				end
				
				if tab.Weapon then
				
					Options:AddOption("Disassemble", function() RunConsoleCommand("ItemHandleUsage", "Disassemble".." "..key);InventoryRefresh();end );
					
				end
				
				if tab.Useable then
					
					Options:AddOption("Use", function() RunConsoleCommand("ItemHandleUsage", "Use".." "..key);InventoryRefresh();end );
					
				end
					
					Options:AddOption("Drop", function() RunConsoleCommand("ItemHandleUsage", "Drop".." "..key);InventoryRefresh();end );
					Options:AddOption("Cancel", function() /* Empty func on purpose*/ end );
					
				Options:Open();
				
			end
	
		InventoryGrid:AddItem(Item);
		
	end
end


